dojo.provide("xray.tools.DropTarget");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");

dojo.requireLocalization("xray.tools", "DropTarget");

dojo.declare("xray.tools.DropTarget",
	[dijit._Widget, dijit._Templated],
	{
	
	iconDrop: dojo.moduleUrl("xray.tools.resources", "iconDropTarget.png"),
		
	templateString: '<div dojoAttachPoint="dropContainer" class="toolsDropContainer"><img src="${iconDrop}" /></div>',	
	
	postCreate: function(){
		dojo.style(this.dropContainer, "opacity", 0.3);
		
		this.messages = dojo.i18n.getLocalization("xray.tools", "DropTarget", this.lang);
		
		dojo.connect(this.dropContainer, "dragenter", this, function(e){
			e.dataTransfer.effectAllowed = "copy";
			dojo.style(this.dropContainer, "opacity", 1);
			e.preventDefault();
		});
		
		dojo.connect(this.dropContainer, "dragover", this, function(e){
			e.preventDefault();
		});
		
		dojo.connect(this.dropContainer, "dragleave", this, function(e){
			e.preventDefault();
			dojo.style(this.dropContainer, "opacity", 0.3);
		});
		
		dojo.connect(this.dropContainer, "drop", this, function(e){
			f = e.dataTransfer.getData( 'application/x-vnd.adobe.air.file-list' );
		    if (f.length > 1){
				alert(this.messages.onlyOneFile);
				return;
			}
			
			f = f[0];
			var extension = f.name.split(".")[1];
			if (extension != "xry"){
				alert(this.messages.typeNotSupported);
				return;
			}
			
			stream = new air.FileStream();
			stream.open( f, air.FileMode.READ );
			data = stream.readMultiByte( stream.bytesAvailable, air.File.systemCharset );
			stream.close();

			console.log(data);

			e.preventDefault();	
		});
	}
});